/**
 * http://www.jxsrfdc.com/Publish/xmgs.aspx
 * 上饶房地产信息网爬虫
 * sunny 2017-06-14
 */

// 工具类
const utils = require('../../utils');

import { Base, Item } from "../base";

const srfdc_url = "http://www.jxsrfdc.com/Publish/xmgs.aspx";

class srfdc_spider extends Base {
    arrs: any[];

    constructor() {
        super();
        this.name = "上饶房地产信息网";
        this.encode = "gb2312";
        this.arrs = [];
        this.timeout = 1000000;
        this.collection = "fdcxx";
    }

    getPageCount(): number {
        var self = this;
        var cnt = 0;
        var data = this.spider.getSync(srfdc_url);
        var $ = this.cheerio.load(data);
        $("a.xmgslinka").each(function (index: number, ele: any) {
            var $a = $(ele);
            var href = $a.attr("href");
            var title = $a.text();
            var item = {};
            (item as any).title = title.trim();
            (item as any).href = href.trim();
            self.arrs.push(item);
            console.log(item);
            cnt++;
        });
        console.log("count=>" + cnt);
        return cnt;
    }


    // 系统回调函数
    getItems(pageIndex: number): Item[] {
        var _url = "http://www.jxsrfdc.com/Publish/" + this.arrs[pageIndex].href;
        var content = this.spider.getSync(_url);
        var $ = this.cheerio.load(content);
        return this.extractItems($, pageIndex, _url);
    };

    private extractItems($: CheerioSelector, pageIndex: number, _url: string): Item[] { // 获取页面中的招标项目信息，$是一个类似jQuery的对象(cheerio)，可以直接按照jQuery方式使用
        var self = this;
        var items: Item[] = [];
        var item = self.buildItem();
        var lpmc: string = "";
        $("table.btitle2 tr").each(function (index, ele) {
            var $tds = $(ele).find("td");
            var key = $($tds[0]).text().trim(); //楼盘名称：
            if (/：/.test(key)) {
                key = key.substring(0, key.length - 1);
            }
            var value = $($tds[1]).text().trim();
            //楼盘名称
            (item as any)[key] = value;
            if (key == "楼盘名称") {
                item["title"] = value;
                lpmc = value;
            }
        });
        (item as any).url = _url;
        items.push(item);
        var urls: string[] = [];
        $("#tab a").each(function (index, ele) {
            var $as = $(ele);
            var _href = $as.attr("href");
            urls.push(_href);
        });
        self.getExtInfomations(lpmc, urls, items);
        return items;
    };


    getExtInfomations(lpmc: string, urls: string[], items: Item[]) {
        var self = this;
        for (var i = 0, j = urls.length; i < j; i++) {
            var url = "http://www.jxsrfdc.com/Publish/" + urls[i];
            var content = self.spider.getSync(url);
            var $ = self.cheerio.load(content);
            var xxdz = $("span#zrzname").eq(0).text();
            $(".detailtab").each(function (index, ele) {
                var item = self.buildItem("srfdcdetail");
                (item as any)["楼盘名称"] = lpmc;
                (item as any)["详细名称"] = xxdz;
                var $e = $(ele);
                var t: string = $($e.find("b")).text().trim();
                (item as any)["房号"] = t;
                (item as any)["title"] = xxdz + " " + t;
                var lfs = $($e.find(".leftpad5px"));
                (item as any)["当前状态"] = lfs.eq(0).text().trim();
                (item as any)["房屋用途"] = lfs.eq(1).text().trim();
                (item as any)["建筑面积"] = lfs.eq(2).text().trim();
                var htbh: string = lfs.eq(3).text().trim();
                (item as any)["合同编号"] = htbh;
                (item as any)["单价"] = lfs.eq(4).text().trim();
                (item as any)["总价"] = lfs.eq(5).text().trim();
                (item as any)["房屋坐落"] = lfs.eq(6).text().trim();
                (item as any)["售出日期"] = 0; //20170909
                if (htbh != "") {
                    (item as any)["售出日期"] = parseInt(htbh.substring(0, 8));
                }
                items.push(item);
            });
        }
    };
}

export = new srfdc_spider();


